//查询列表
let listurl = 'https://wxapi.mgsco.cn:8085/wxapi/Task/GetQdlist'
//获取车辆
let carurl = 'https://wxapi.mgsco.cn:8085/wxapi/Task/GetUserCarlist'
//详情
let detailsurl = 'https://wxapi.mgsco.cn:8085/wxapi/Task/GetLdinfo'
//下单
let gourl = 'https://wxapi.mgsco.cn:8085/wxapi/Task/atMqPDLRgGqCxFESgQNJ'

//登陆 
let loginurl = 'https://wxapi.mgsco.cn:8085/wxapi/S_User_Login/CheckUserName'
//设置信息
let setinfourl = 'https://wxapi.mgsco.cn:8085/wxapi/UserInfo/Setwxinfo'
//token
let authorization = ''
//数据类型
let contenttype = 'application/x-www-form-urlencoded'
//agent
let useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) IOS'

// function getusergps() {
// 	/*
// 	{
// 	“latitude”:"39.97239"，
// 	“speed”:"0.0",
// 	"accuracy":"60.0",
// 	"longitude":"116.38212",
// 	"errMsg":"getLocation:ok"
// 	}
// 	*/
// 	var e = this;
// 	t.getLocation({
// 		type: "wgs84",
// 		success: function(t) {
// 			var n = t.latitude,
// 				i = t.longitude,
// 				a = t.speed,
// 				o = t.accuracy;
// 			e.remarkstr = "经度：" + i + "纬度：" + n + "速度：" +
// 				a + "精确度：" + o
// 		}
// 	})
// }
//md5加密
function md5encry(e) {
	return function(e) {
		for (var t, r = "0123456789abcdef", n = "", a = 0; a < e.length; a++) t = e
			.charCodeAt(a), n += r.charAt(t >>> 4 & 15) + r.charAt(15 & t);
		return n
	}(function(e) {
		return function(e) {
			for (var t = "", r = 0; r < 32 * e.length; r += 8) t += String
				.fromCharCode(e[r >> 5] >>> r % 32 & 255);
			return t
		}(function(e, t) {
			e[t >> 5] |= 128 << t % 32, e[14 + (t + 64 >>> 9 << 4)] = t;
			for (var r = 1732584193, n = -271733879, u = -1732584194, c =
					271733878, f = 0; f < e.length; f += 16) {
				var h = r,
					d = n,
					p = u,
					v = c;
				r = a(r, n, u, c, e[f + 0], 7, -680876936), c = a(c, r, n,
						u, e[f + 1], 12, -389564586), u = a(u, c, r, n, e[
						f + 2], 17, 606105819), n = a(n, u, c, r, e[f + 3],
						22, -1044525330), r = a(r, n, u, c, e[f + 4], 7, -
						176418897), c = a(c, r, n, u, e[f + 5], 12,
						1200080426), u = a(u, c, r, n, e[f + 6], 17, -
						1473231341), n = a(n, u, c, r, e[f + 7], 22, -
						45705983), r = a(r, n, u, c, e[f + 8], 7,
						1770035416), c = a(c, r, n, u, e[f + 9], 12, -
						1958414417), u = a(u, c, r, n, e[f + 10], 17, -
						42063), n = a(n, u, c, r, e[f + 11], 22, -
						1990404162), r = a(r, n, u, c, e[f + 12], 7,
						1804603682), c = a(c, r, n, u, e[f + 13], 12, -
						40341101), u = a(u, c, r, n, e[f + 14], 17, -
						1502002290), n = a(n, u, c, r, e[f + 15], 22,
						1236535329), r = i(r, n, u, c, e[f + 1], 5, -
						165796510), c = i(c, r, n, u, e[f + 6], 9, -
						1069501632), u = i(u, c, r, n, e[f + 11], 14,
						643717713), n = i(n, u, c, r, e[f + 0], 20, -
						373897302), r = i(r, n, u, c, e[f + 5], 5, -
						701558691), c = i(c, r, n, u, e[f + 10], 9,
						38016083), u = i(u, c, r, n, e[f + 15], 14, -
						660478335), n = i(n, u, c, r, e[f + 4], 20, -
						405537848), r = i(r, n, u, c, e[f + 9], 5,
						568446438), c = i(c, r, n, u, e[f + 14], 9, -
						1019803690), u = i(u, c, r, n, e[f + 3], 14, -
						187363961), n = i(n, u, c, r, e[f + 8], 20,
						1163531501), r = i(r, n, u, c, e[f + 13], 5, -
						1444681467), c = i(c, r, n, u, e[f + 2], 9, -
						51403784), u = i(u, c, r, n, e[f + 7], 14,
						1735328473), n = i(n, u, c, r, e[f + 12], 20, -
						1926607734), r = o(r, n, u, c, e[f + 5], 4, -
						378558), c = o(c, r, n, u, e[f + 8], 11, -2022574463),
					u = o(u, c, r, n, e[f + 11], 16, 1839030562), n = o(n,
						u, c, r, e[f + 14], 23, -35309556), r = o(r, n, u,
						c, e[f + 1], 4, -1530992060), c = o(c, r, n, u, e[
						f + 4], 11, 1272893353), u = o(u, c, r, n, e[f + 7],
						16, -155497632), n = o(n, u, c, r, e[f + 10], 23, -
						1094730640), r = o(r, n, u, c, e[f + 13], 4,
						681279174), c = o(c, r, n, u, e[f + 0], 11, -
						358537222), u = o(u, c, r, n, e[f + 3], 16, -
						722521979), n = o(n, u, c, r, e[f + 6], 23,
						76029189), r = o(r, n, u, c, e[f + 9], 4, -
						640364487), c = o(c, r, n, u, e[f + 12], 11, -
						421815835), u = o(u, c, r, n, e[f + 15], 16,
						530742520), n = o(n, u, c, r, e[f + 2], 23, -
						995338651), r = l(r, n, u, c, e[f + 0], 6, -
						198630844), c = l(c, r, n, u, e[f + 7], 10,
						1126891415), u = l(u, c, r, n, e[f + 14], 15, -
						1416354905), n = l(n, u, c, r, e[f + 5], 21, -
						57434055), r = l(r, n, u, c, e[f + 12], 6,
						1700485571), c = l(c, r, n, u, e[f + 3], 10, -
						1894986606), u = l(u, c, r, n, e[f + 10], 15, -
						1051523), n = l(n, u, c, r, e[f + 1], 21, -
						2054922799), r = l(r, n, u, c, e[f + 8], 6,
						1873313359), c = l(c, r, n, u, e[f + 15], 10, -
						30611744), u = l(u, c, r, n, e[f + 6], 15, -
						1560198380), n = l(n, u, c, r, e[f + 13], 21,
						1309151649), r = l(r, n, u, c, e[f + 4], 6, -
						145523070), c = l(c, r, n, u, e[f + 11], 10, -
						1120210379), u = l(u, c, r, n, e[f + 2], 15,
						718787259), n = l(n, u, c, r, e[f + 9], 21, -
						343485551), r = s(r, h), n = s(n, d), u = s(u, p),
					c = s(c, v)
			}
			return Array(r, n, u, c)
		}(function(e) {
			for (var t = Array(e.length >> 2), r = 0; r < t.length; r++)
				t[r] = 0;
			for (r = 0; r < 8 * e.length; r += 8) t[r >> 5] |= (255 & e
				.charCodeAt(r / 8)) << r % 32;
			return t
		}(e), 8 * e.length))
	}(function(e) {
		var t, r, n = "",
			a = -1;
		for (; ++a < e.length;) t = e.charCodeAt(a), r = a + 1 < e.length ? e
			.charCodeAt(a + 1) : 0, 55296 <= t && t <= 56319 && 56320 <= r &&
			r <= 57343 && (t = 65536 + ((1023 & t) << 10) + (1023 & r), a++),
			t <= 127 ? n += String.fromCharCode(t) : t <= 2047 ? n += String
			.fromCharCode(192 | t >>> 6 & 31, 128 | 63 & t) : t <= 65535 ? n +=
			String.fromCharCode(224 | t >>> 12 & 15, 128 | t >>> 6 & 63, 128 |
				63 & t) : t <= 2097151 && (n += String.fromCharCode(240 | t >>>
				18 & 7, 128 | t >>> 12 & 63, 128 | t >>> 6 & 63, 128 | 63 &
				t));
		return n
	}(e)))


	function n(e, t, r, n, a, i) {
		return s(function(e, t) {
			return e << t | e >>> 32 - t
		}(s(s(t, e), s(n, i)), a), r)
	}

	function a(e, t, r, a, i, o, l) {
		return n(t & r | ~t & a, e, t, i, o, l)
	}

	function i(e, t, r, a, i, o, l) {
		return n(t & a | r & ~a, e, t, i, o, l)
	}

	function o(e, t, r, a, i, o, l) {
		return n(t ^ r ^ a, e, t, i, o, l)
	}

	function l(e, t, r, a, i, o, l) {
		return n(r ^ (t | ~a), e, t, i, o, l)
	}

	function s(e, t) {
		var r = (65535 & e) + (65535 & t);
		return (e >> 16) + (t >> 16) + (r >> 16) << 16 | 65535 & r
	}
}


//随机字符
function random_char(e) {
	for (var t = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b",
			"c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
			"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
		], r = "", n = 0; n < e; n++) {
		r += t[Math.ceil(35 * Math.random())]
	}
	return r
}


//获取随机时间吧
function secretdate() {
	return "timestamp=" + (new Date).getTime() + "&nonce=" + random_char(32)
}

function guid(len = 32, firstU = true, radix = null) {
	const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
	const uuid = []
	radix = radix || chars.length

	if (len) {
		// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
		for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
	} else {
		let r
		// rfc4122标准要求返回的uuid中,某些位为固定的字符
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
		uuid[14] = '4'

		for (let i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
			}
		}
	}
	// 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
	if (firstU) {
		uuid.shift()
		return `u${uuid.join('')}`
	}
	return uuid.join('')
}

var userinfo = {}

//用户登陆函数
function login(username, password, callback) {
	let deviceid = guid(32, !0, 62)
	userinfo['deviceid'] = deviceid
	axios.post(loginurl, {
		username: username,
		password: password,
		deviceid: deviceid,
		remark: ""
	}).then(e => {
		authorization = `Bearer ${e.data.token}`
		Object.assign(userinfo, e.data)
		callback(e)
	})
}








function jiami(data) {
	var encrypt = new JSEncrypt();

	// 设置公钥，这里用示例公钥，请替换成你的真实公钥
	var publicKey =
		"-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvnnA4C8lJgXgQrzzt7PA8SJ9IqbkDhHrVxmfjU+tSvHy+oxBBh4IkKwupBjaPawI/23Ep2DAHD4FptQt4iUr33nnCWJFjFI+ic97BentcsrP4RFkfoyurID/YyUsa/DZZPg8xDmmswbRDeNscKnfjuG5BQiQBXJK3VpAdZrUOBmWNPf7WhZZCjKh3RojG8GeSQwJTjMCmXUkk+gwSTBSNCK5a3HgToQ5L4bJpg7zljVLR1yCg9As/yW8sqkhlvsXLwBo7FNWebA+/TJs2+M1HgeNx8O0O9td+Kv1jYwSliWEubf959x75xG9KvTijOrK0Gz6qsHpc6LgNpCScAe+PQIDAQAB-----END PUBLIC KEY-----"

	// 设置公钥
	encrypt.setPublicKey(publicKey);

	//要加密的数据
	var plaintext = JSON.stringify(data);

	// 使用公钥进行加密
	var encrypted = encrypt.encrypt(plaintext);

	// 加密后的数据
	return encrypted;
}


//查询列表
async function chaxun(userid, wxuserid) {

	let tid = {
		userid: userid,
		lx: 3,
		wxuserid: wxuserid,
		page: 1,
		cartypestr: 0
	}
	let result = await axios.post(listurl, {
		sendstr: jiami(tid),
	}, {
		headers: {
			// 'User-Agent': useragent,
			'Authorization': authorization,
			'Content-Type': contenttype
		}
	})
	return result.data
}

//查询详情

async function xiangqing(queryid, wxuserid) {
	//tid: '1006535187',
	//flag: 1,
	//wxuserid: 'oSmlb48FBkWbAeOOMtXAj4R00SiM'
	let e = {
		tid: queryid,
		flag: 1,
		wxuserid: wxuserid
	}
	let result = await axios.post(detailsurl, {
		tid: jiami(e),
	}, {
		headers: {
			// 'User-Agent': useragent,
			'Authorization': authorization,
			'Content-Type': contenttype
		}
	})
	return result.data
}


//获取车辆
async function getcar(userid, wxuserid) {
	let e = {
		userid: userid,
		wxuserid: wxuserid
	}
	let result = await axios.post(carurl, {
		userid: jiami(e),
	}, {
		headers: {
			// 'User-Agent': useragent,
			'Authorization': authorization,
			'Content-Type': contenttype
		}
	})
	return result.data
}


async function getgps(longitude, latitude, speed, accuracy) {
	return "经度：" + longitude + "纬度：" + latitude + "速度：" +
		speed + "精确度：" + accuracy
}

/*
userid	158254
validate	CN31_Njrk26rkiYk7Zo9We1UiA-iDbCZ2pFuM4gXsR.15Y4ll1pVQLWaYhipNGaGom_cWr5YR9BN1451KJoaeSxGJVq8C0NJoR2X.LbKG.Fwna.ADRY.gXP-Z9ZFhw-pXjNpPgfnak-lVLabGYqbBIi4qiYclyVIOA-OqwmfQLktEL_QzkTJKSvREryzcT8dGVA7kIAdortx.y5f_PqFNlm2oeP-45L7H28TaVodCZgIy2PLCb5z.57fOQcA-ZpZqefIJ
verifystr	fa06jT9M+CXOYDBnwzqc94Ewh1cAqSttkurIvaNDOsE+7nWIKGlQcnO2hQPTF2+xaksHfyxvk+clom6s+wc8aU4I2tBxoyjFN4M8rfX1VlEVs6KrJquKM1ckssAskwKVkGwYCsj8jQYlCzlZq0S3fkmJQFYVQCpMuk77cVRF0HSKlDpQD8sN1ck3XlwrtfZ7agQbs9umh1GqCrM9kM7kMfBeFlx1eTBNElt0bhR5IK8VQlca35b9iRFGcUrzqiQGvuHeNJyxPRVgzh0jSPSxE0PPBdgcy0S38nbaWYAuEFauS5KmFGghoRZxBznxIRbCuPKJYOu9gFaHay7VXX11pQ==
secretdate	timestamp=1703207793972&nonce=inqf16oe76emqamla5z1k5kp5lmj1ghc
secreted	a55fd6a1620d6446ebd5dd07735d1f0c
*/
async function qiangdan(queryid, carnum, userid, wxuserid, keystr, vali) {

	//要发送的数据
	let n = queryid,
		i = carnum,
		o = userid,
		s = wxuserid,
		r = (this.logstr = "", this.keystr = keystr, this.remarkstr = await getgps('112.36952', '37.64456', '0.0',
			'65'));
	// f = this.flag;
	// "" == this.remarkstr && that.getusergps();
	var h = secretdate(),
		p = md5encry(s + n),
		g = {
			cymxid: n,
			carid: i,
			flag: 1,
			deviceid: s + "~" + r
		},
		m = jiami(g)
	console.log(h);
	console.log(p);
	console.log(g);
	console.log(m);
	let result = await axios.post(gourl, {
		userid: o,
		validate: vali,
		verifystr: m,
		secretdate: h,
		secreted: p
	}, {
		headers: {
			// 'User-Agent': useragent,
			'Authorization': authorization,
			'Content-Type': contenttype
		}
	})
	return result

}